Param(
  [string]$OutputDir = "public",
  [string]$Text = "FH",
  [string]$Color1 = "#2A82E4",
  [string]$Color2 = "#195BA1"
)

Add-Type -AssemblyName System.Drawing

if (!(Test-Path $OutputDir)) {
  New-Item -ItemType Directory -Path $OutputDir | Out-Null
}

# Convert hex colors to System.Drawing.Color
function Convert-HexToColor {
  param([string]$hex)
  $hex = $hex.TrimStart('#')
  if ($hex.Length -eq 6) {
    $r = [Convert]::ToByte($hex.Substring(0,2), 16)
    $g = [Convert]::ToByte($hex.Substring(2,2), 16)
    $b = [Convert]::ToByte($hex.Substring(4,2), 16)
    return [System.Drawing.Color]::FromArgb(255, $r, $g, $b)
  }
  return [System.Drawing.Color]::FromArgb(255, 42, 130, 228)
}

$color1 = Convert-HexToColor $Color1
$color2 = Convert-HexToColor $Color2

Write-Host "🎨 Generating icons with text: '$Text'" -ForegroundColor Green
Write-Host "   Colors: $Color1 -> $Color2" -ForegroundColor Cyan

# Function to create icon at specific size
function Create-Icon {
  param([int]$size)
  
  $bitmap = New-Object System.Drawing.Bitmap $size, $size
  $graphics = [System.Drawing.Graphics]::FromImage($bitmap)
  
  $graphics.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::HighQuality
  $graphics.TextRenderingHint = [System.Drawing.Text.TextRenderingHint]::ClearTypeGridFit
  
  $rect = New-Object System.Drawing.Rectangle 0, 0, $size, $size
  $rectF = [System.Drawing.RectangleF]::FromLTRB(0, 0, $size, $size)
  
  # Create gradient brush
  $brush = New-Object System.Drawing.Drawing2D.LinearGradientBrush ($rect, $color1, $color2, 45.0)
  $graphics.FillRectangle($brush, 0, 0, $size, $size)
  
  # Calculate font size based on icon size
  $fontSize = [Math]::Max(12, $size * 0.22)
  $font = New-Object System.Drawing.Font ("Segoe UI", $fontSize, [System.Drawing.FontStyle]::Bold)
  $format = New-Object System.Drawing.StringFormat
  $format.Alignment = [System.Drawing.StringAlignment]::Center
  $format.LineAlignment = [System.Drawing.StringAlignment]::Center
  
  # Shadow offset
  $shadowOffset = [Math]::Max(1, $size * 0.008)
  
  # Draw shadow
  $shadowBrush = New-Object System.Drawing.SolidBrush ([System.Drawing.Color]::FromArgb(90, 0, 0, 0))
  $offsetRectF = [System.Drawing.RectangleF]::FromLTRB($shadowOffset, $shadowOffset, $size + $shadowOffset, $size + $shadowOffset)
  $graphics.DrawString($Text, $font, $shadowBrush, $offsetRectF, $format)
  
  # Draw main text
  $textBrush = New-Object System.Drawing.SolidBrush ([System.Drawing.Color]::FromArgb(255, 255, 255, 255))
  $graphics.DrawString($Text, $font, $textBrush, $rectF, $format)
  
  $graphics.Dispose()
  return $bitmap
}

# Create main PNG (1024x1024)
Write-Host "📱 Creating main PNG icon..." -ForegroundColor Yellow
$mainBitmap = Create-Icon 1024
$pngPath = Join-Path $OutputDir "icon.png"
$mainBitmap.Save($pngPath, [System.Drawing.Imaging.ImageFormat]::Png)
Write-Host "✅ Generated: $pngPath" -ForegroundColor Green

# Create ICO with multiple sizes
Write-Host "🖼️  Creating multi-size ICO..." -ForegroundColor Yellow
$icoSizes = @(16, 24, 32, 48, 64, 128, 256)
$icoBitmaps = @()

foreach ($size in $icoSizes) {
  $icoBitmaps += Create-Icon $size
}

# Create ICO file with multiple sizes
$icoPath = Join-Path $OutputDir "icon.ico"

# Use the largest bitmap as the base for ICO
$baseBitmap = $icoBitmaps[-1] # 256x256
$icon = [System.Drawing.Icon]::FromHandle($baseBitmap.GetHicon())

# Save ICO file
$fs = [System.IO.File]::Open($icoPath, [System.IO.FileMode]::Create)
$icon.Save($fs)
$fs.Close()

# Clean up
$icon.Dispose()
$mainBitmap.Dispose()
foreach ($bitmap in $icoBitmaps) {
  $bitmap.Dispose()
}

Write-Host "✅ Generated: $icoPath" -ForegroundColor Green
Write-Host ""
Write-Host "🎉 Icon generation completed!" -ForegroundColor Green
Write-Host "   PNG: $pngPath" -ForegroundColor White
Write-Host "   ICO: $icoPath" -ForegroundColor White
Write-Host ""
Write-Host "💡 Usage examples:" -ForegroundColor Cyan
Write-Host "   .\scripts\generate-icons.ps1" -ForegroundColor Gray
Write-Host "   .\scripts\generate-icons.ps1 -Text 'FH'" -ForegroundColor Gray
Write-Host "   .\scripts\generate-icons.ps1 -Text 'FH' -Color1 '#FF6B6B' -Color2 '#4ECDC4'" -ForegroundColor Gray
Write-Host ""
Write-Host "ℹ️  For macOS .icns, run: pnpm run generate-icons" -ForegroundColor Yellow


